package com.hm.service.Impl;

import com.hm.dao.MenuMapper;
import com.hm.dao.StudentMapper;
import com.hm.dao.TeacherMapper;
import com.hm.pojo.Menu;
import com.hm.pojo.Student;
import com.hm.pojo.Teacher;
import com.hm.service.TeacherService;
import com.hm.utils.MybatisUtil;
import com.hm.utils.UuidUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TeacherServiceImpl implements TeacherService {

    private TeacherMapper teacherMapper ;

    @Override
    public Teacher login(String account, String password) {
        SqlSession sqlSession = MybatisUtil.openSession();
        teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        MenuMapper mapper = sqlSession.getMapper(MenuMapper.class);

        Teacher login = null;
        try {
            login = teacherMapper.login(account, password);
            List<Menu> userList = mapper.getAllList();
            for (Menu menu : userList) {
                menu.setChildren(mapper.getbyid(menu.getId()));
            }
            for (Menu menu : userList) {
                if(menu.getChildren().isEmpty()){
                    menu.setChildren(null);
                }
            }
            login.setToken(UuidUtil.getUuid());
            login.setMenu(userList);


            //开启事务
            //sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            sqlSession.rollback();
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return login;
    }
}
